<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\Carousel;
use app\common\model\News;
use app\common\model\ProductCate;
use app\common\model\Product;
use app\common\model\User;
use app\common\model\Coupon;
use app\common\model\Usercoupon;

use think\facade\Cache;
use think\facade\View;
use app\common\tool\Result;
use app\common\controller\Config;

class Index extends Base
{

    public function index()
    {
        $token = $this->request->header('token');
        $coupon_open=Config::lists()['coupon_open'];
        $system=Config::lists();
        if(!empty($token)){
            $user=User::where(['token'=>$token])->find();
            if(!empty($user)){
                $Usercount=Usercoupon::where(['uid'=>$user->id,'is_del'=>0])->count();
                $c_map[] = ['level_ids', 'like', '%' . $user->level_id . '%'];
            }else{
                $Usercount=0; 
            }
        }else{
            $Usercount=0;  
        }
        $carousel = Carousel::getList(['type' => 1], true, 'sort', [], 'id,images,link');
        $c_map[] = ['is_del', '=', 0];
        $c_map[] = ['status', '=', 1];
        $c_map[] = ['types', '=', 1];
        $c_list = Coupon::where($c_map)->select();
        $Couponlist= $c_list->isEmpty() ? '' : $c_list;
        $adv = Carousel::getList(['type' => 2], true, 'sort', [], 'id,images,link');
        $cate = ProductCate::where(['status' => 1, 'is_del' => 0, 'pid' => 0,'index_show'=>1])->field('id,title,icon,bg')->select();
        $catelist = ProductCate::where(['status' => 1,'is_show'=>1,'is_del' => 0, 'pid' => 0])->field('id,title,icon,bg')->order('sort asc')->select();
        foreach($catelist as $k=>$v){
            $son=Product::where(['status' => 1, 'is_del' => 0,'cate_1'=>$v['id']])->field('id,title,describe,price,vip_price,icon,stock,sales')->order('sort desc')->limit(14)->select();
            foreach($son as &$v1){
                $levelinfo =User::getPrice($this->token,$v1->vip_price);
                $v1->vip_price = $levelinfo['price'];
                $v1->level_title = $levelinfo['title'];
            }
            $v['son'] = $son;
            $catelist[$k]=$v;
        }
        //$news = News::getList(['type' => 2], true, 'sort', [], 'id,title');
        $news = News::where(['is_del'=>0])->distinct(true)->orderRaw('rand()')->order('create_time desc')->limit(3)->column('title');
        $product=Product::where(['status' => 1, 'is_del' => 0,'pick'=>1])->field('id,title,describe,price,vip_price,icon,stock,sales')->order('sort desc')->select();
        foreach($product as &$v){
            $level =User::getPrice($this->token,$v->vip_price);
            $v->vip_price = $level['price'];
            $v->level_title = $level['title'];
        }
        $result = [
            'banner' => $carousel,
            'adv' => $adv,
            'news' =>  implode(' ；',$news),
            'product' => $product,
            'cate' => $cate,
            'catelist' => $catelist,
            'system' => $system,
            'Usercount'=>$Usercount,
            'Couponlist'=>$Couponlist,
            'coupon_open'=>intval($coupon_open)
        ];
        return Result::success('获取数据成功！',$result);
    }

    public function communal()
    {
        $type = $this->request->param('type');
        if($type==''){
            return Result::fail('获取内容失败！');
        }
        $title = ['company' => '公司介绍', 'register_msg' => '注册协议','privacy_msg'=>'隐私协议','user_msg'=>'用户协议','buy_msg'=>'购买流程','level_msg'=>'等级说明'];
        $content=Config::lists()[$type];
        $url = 'https://' . $_SERVER['SERVER_NAME'] . '/uploads/';
        $content = htmlspecialchars_decode(str_replace('/uploads/', $url, $content));
        $content = str_replace('\\', '', $content);
        $data=[
            'title' => $title[$type],
            'content' => $content
        ];
        return Result::success('获取数据成功！',$data);
    }
}
